package com.example.q_kang.hhh.login;


import android.content.Context;
import android.content.Intent;
import android.support.constraint.Group;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.hjq.toast.ToastUtils;
import com.sskj.base.MVPActivity;
import com.sskj.base.base.App;
import com.sskj.base.bean.RegisterBean;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.ToolBarLayout;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/04/11
 */
public class ForgetPwdActivity extends MVPActivity<ForgetPwdPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.rl_pwd_see)
    CheckBox rlPwdSee;
    @BindView(R.id.rl_confirm_pwd_see)
    CheckBox rlConfirmPwdSee;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_mobile_code)
    EditText etMobileCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.btn_find_pwd)
    Button btnFindPwd;
    @BindView(R.id.group_sms)
    Group group_sms;
    private String isShowSMS = "1";

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public ForgetPwdPresenter getPresenter() {
        return new ForgetPwdPresenter();
    }

    @Override
    public void initView() {
        mPresenter.getIsNeedSMS();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        rlPwdSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        rlConfirmPwdSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etConfirmPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etConfirmPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        ClickUtil.click(300, tvGetCode, view -> {
            if (TextUtils.isEmpty(etMobile.getText())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity1));
                return;
            }
            if (!CheckUtil.isMobile(etMobile.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity2));
                return;
            }
            mPresenter.sendCode(etMobile.getText().toString().trim());
        });
        ClickUtil.click(300, btnFindPwd, view -> {
            if (TextUtils.isEmpty(etMobile.getText())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity1));
                return;
            }
            if (!CheckUtil.isMobile(etMobile.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity2));
                return;
            }
            if (isShowSMS.equals("1")) {
                if (TextUtils.isEmpty(etMobileCode.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity5));
                    return;
                }
            }
            if (TextUtils.isEmpty(etPwd.getText())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity6));
                return;
            }
            if (etPwd.getText().length() < 6) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity7));
                return;
            }
            if (!etPwd.getText().toString().equals(etConfirmPwd.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaForgetPwdActivity8));
                return;
            }
            mPresenter.resetPwd(etMobile.getText().toString(),
                    isShowSMS.equals("1") ? etMobileCode.getText().toString() : "",
                    etPwd.getText().toString());
        });

    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ForgetPwdActivity.class);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void resetSuccess() {
        finish();
    }

    public void isShowSMS(RegisterBean registerBean) {

        if (registerBean == null) {
            return;
        }
        if (registerBean.getValue().equals("1")) {
            this.isShowSMS = "1";
            group_sms.setVisibility(View.VISIBLE);
        }
        if (registerBean.getValue().equals("2")) {
            this.isShowSMS = "2";
            group_sms.setVisibility(View.GONE);
        }


    }
}
